package com.joplus.housekeeper.ui.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;

import com.joplus.housekeeper.utils.DensityUtils;
import com.joplus.housekeeper.utils.ScreenUtils;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2016/8/26.
 */
public class AutoHidePopLayout extends RelativeLayout implements View.OnClickListener {

    ViewGroup viewGroup;
    int height;
    View button;
    Boolean ok = true;
    Boolean isShow=false;
    Context context;



    int dropupHeight;//弹出来的距离顶点的高度

    public AutoHidePopLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        dropupHeight= DensityUtils.dip2px(context,200);
        this.context=context;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewGroup = (ViewGroup) getChildAt(2);
        button = (View) getChildAt(1);
        height = MeasureSpec.getSize(heightMeasureSpec);
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) viewGroup.getLayoutParams();
        params.height = height;
        viewGroup.setLayoutParams(params);
        button.setOnClickListener(this);
        this.setOnClickListener(this);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getY() < dropupHeight&&isShow) {

                   return true;
                }

                break;

        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getY() < dropupHeight&&isShow) {
                    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    //imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    imm.hideSoftInputFromWindow(this.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                    isShow=false;
                    ObjectAnimator.ofFloat(viewGroup, "translationY", dropupHeight, height).setDuration(400).start();

                }

                break;

        }

        return super.onTouchEvent(event);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (ok) {
            ok = false;
            //只在界面初始化的时候移动布局
            ObjectAnimator.ofFloat(viewGroup, "translationY", height, height).setDuration(0).start();
        }

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == button.getId()&&!isShow) {
            isShow=true;
            ObjectAnimator.ofFloat(viewGroup, "translationY", height, dropupHeight).setDuration(400).start();
        }
    }



}
